package com.gongpingjialoan.activity.loans;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.api.Piont;
import com.gongpingjialoan.bean.LoanFinishEB;
import com.gongpingjialoan.bean.LoanStatusData;
import com.gongpingjialoan.bean.VipBuycarBean;
import com.gongpingjialoan.manage.PointRecordManage;
import com.gongpingjialoan.utility.LoanUtil;
import com.gongpingjialoan.view.LoanCarModelView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

/**
 * 审核结果 //初始额度
 */
public class AuditResultActivity extends BaseActivity implements View.OnClickListener{
    private TextView submit,loan_amountT;
    private LoanCarModelView car_mode_layout;
    //根据问卷选项算出后的价格
    private int current_loan_price = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audit_result);
        EventBus.getDefault().register(this);
        initView();
    }

    private void initView() {
        PointRecordManage.getInstance().addPoint(mySelf, Piont.LoanInitCreditView);
        setTitle("初始额度");
        car_mode_layout = (LoanCarModelView) findViewById(R.id.car_mode_layout);
        submit = (TextView) findViewById(R.id.submit);
        loan_amountT = (TextView) findViewById(R.id.loan_amount);

        submit.setOnClickListener(this);

        try {
            current_loan_price = Integer.valueOf(LoanStatusData.getInstance().getCredit_limit());
        }catch (NumberFormatException e){

        }
        setCurrentPriceText();
    }

    //刷新页面当前可贷额度
    private void setCurrentPriceText(){
        int current_price = LoanUtil.computingAmount(current_loan_price ,car_mode_layout.getCarPrice());
        java.text.DecimalFormat df = new java.text.DecimalFormat("##,###,###");
        loan_amountT.setText(df.format(current_price));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.submit:
                PointRecordManage.getInstance().addPoint(mySelf, Piont.CreditChooseApply);
                Intent it = new Intent(mySelf,LoanCertificateActivity.class);
                startActivity(it);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void finish() {
        super.finish();
        PointRecordManage.getInstance().addPoint(mySelf, Piont.LoanInitCreditBack);
    }

    @Subscribe
    public void onEventMainThread(VipBuycarBean vipBuycarBean) {
        PointRecordManage.getInstance().addPoint(mySelf, Piont.CreditChooseClick);
        car_mode_layout.onBrandSelceted(vipBuycarBean);
        setCurrentPriceText();
    }

    @Subscribe
    public void onEventMainThread(LoanFinishEB event) {
        finish();
    }
}
